//
//  UIButton+Extension.swift
//  zhengwutong
//
//  Created by neo on 2017/4/10.
//  Copyright © 2017年 wsg. All rights reserved.
//

import UIKit
class RoundRectButton: UIButton {
    override func awakeFromNib() {
        super.awakeFromNib()
        self.layer.masksToBounds = true
        self.layer.cornerRadius = 3
        
    }
}
class RoundRectView: UIView {
    override func awakeFromNib() {
        super.awakeFromNib()
        self.layer.masksToBounds = true
        self.layer.cornerRadius = 3
        
    }
}
class MessageButton: UIButton {
    private var countLabel:UILabel!
    override init(frame: CGRect) {
        super.init(frame: frame)
        countLabel = UILabel(frame: CGRect.zero)
        countLabel.backgroundColor = UIColor.red
        countLabel.font = UIFont.systemFont(ofSize: 12)
        countLabel.textColor = .white
        countLabel.textAlignment = .center
        self.addSubview(countLabel)
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        countLabel = UILabel(frame: CGRect.zero)
        countLabel.backgroundColor = UIColor.red
        countLabel.font = UIFont.systemFont(ofSize: 12)
        countLabel.textColor = .white
        countLabel.textAlignment = .center
        self.addSubview(countLabel)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        countLabel.text = self.count > 0 ? "\(self.count)" : ""
        countLabel.sizeToFit()
        if countLabel.frame.size.width < countLabel.frame.size.height{
            var f = countLabel.frame
            f.size.width = f.size.height
            countLabel.frame = f
        }
        countLabel.center = CGPoint(x: self.frame.size.width-countLabel.frame.size.width/2, y: countLabel.frame.size.height/2)
        countLabel.layer.roundWithRadius(countLabel.frame.size.height/2)
    }
    var count:Int = 0{
        didSet{
            setNeedsLayout()
        }
    }
}
class BorderButton: UIButton {
    override func awakeFromNib() {
        super.awakeFromNib()
        self.layer.borderColor = UIColor.black.cgColor
        self.layer.borderWidth = 0.5
        
    }
}
extension UIButton {
    func fitSize() -> Void {
        if self.currentImage != nil {
            let size = (self.currentImage)!.size
            let top : CGFloat = (self.et_height - size.height - (self.titleLabel?.font.pointSize)! - 18)/2
            let titlesize = self.currentTitle?.size(withAttributes: [NSAttributedString.Key.font:self.titleLabel?.font ?? UIFont.systemFont(ofSize: 14)])
            
            
            self.imageEdgeInsets = UIEdgeInsets.init(top: top, left: (titlesize?.width)!, bottom: self.et_height - top - (size.height), right: 0)
            self.titleEdgeInsets = UIEdgeInsets.init(top: size.height + 18, left: -size.width, bottom: 0, right: 0)
        }
    }
}
